﻿#include "button.h"

#define BUTTON_WIDTH 234
#define BUTTON_HEIGHT 117
#define FONT_SIZE 25

#include <QPainter>
#include <QFont>
#include <QPen>
#include <QDebug>

Button::Button(QGraphicsScene *scene, QString text):
    m_scene(scene),
    m_text(text)
{
    m_scene->addItem(this);
    //this->setFlag(QGraphicsItem::ItemIsMovable);
    drawFace();
    initAnimation();

    setAcceptHoverEvents(true);
}

Button::~Button()
{

}

QRectF Button::boundingRect() const{
    return QRectF(-BUTTON_WIDTH / 2, -BUTTON_HEIGHT / 2,
                  BUTTON_WIDTH, BUTTON_HEIGHT);
}

void Button::paint(QPainter *painter,
           const QStyleOptionGraphicsItem *option,
           QWidget *widget){
    // 镇压宏
    Q_UNUSED(option);
    Q_UNUSED(widget)

    painter->setRenderHint(QPainter::Antialiasing, true);
    painter->setRenderHint(QPainter::SmoothPixmapTransform, true);

    painter->drawImage(boundingRect(), m_face->toImage());

}

void Button::drawFace(){

    m_face = new QPixmap(BUTTON_WIDTH, BUTTON_HEIGHT);
    m_face->fill(Qt::transparent);

    QPainter painter(m_face);
    painter.setRenderHint(QPainter::Antialiasing); // 抗锯齿，使绘制更平滑
    QString strFace(":/Monster_Kill/Res/Img/Button.png");

    painter.drawPixmap(0, 0, QPixmap(strFace).
                      scaled(QSize(BUTTON_WIDTH, BUTTON_HEIGHT),
                             Qt::KeepAspectRatio, Qt::SmoothTransformation));
    QFont font("华文彩云", FONT_SIZE);
    QPen pen(QColor(88, 75, 83));
    font.setBold(true);
    pen.setCapStyle(Qt::RoundCap);

    painter.setPen(pen);
    painter.setFont(font);
    painter.drawText(QPoint(32, BUTTON_HEIGHT / 2), m_text);

    painter.end();
}

void Button::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    Q_UNUSED(event);
    m_selectAnimation->start();
}

void Button::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    Q_UNUSED(event);
    m_leaveAnimation->start();
}

void Button::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    Q_UNUSED(event);
    m_pressAnimation->setEndValue(QPoint(this->pos().x(), this->pos().y() + 3));
    m_backAnimation->setEndValue(pos());
    Button_Press->start();
}

bool Button::collidesWithItem(const QGraphicsItem *other, Qt::ItemSelectionMode mode) const
{
    Q_UNUSED(other);
    Q_UNUSED(mode);

    return false;
}

void Button::initAnimation()
{
    m_selectAnimation = new QPropertyAnimation(this, "scale");
    m_selectAnimation->setDuration(166);
    m_selectAnimation->setEndValue(1.02);
    m_selectAnimation->setEasingCurve(QEasingCurve::Linear);

    m_leaveAnimation = new QPropertyAnimation(this, "scale");
    m_leaveAnimation->setDuration(166);
    m_leaveAnimation->setEndValue(1);
    m_leaveAnimation->setEasingCurve(QEasingCurve::Linear);

    Button_Press = new QSequentialAnimationGroup(this);
    m_pressAnimation = new QPropertyAnimation(this, "pos", Button_Press);
    m_pressAnimation->setDuration(108);
    m_pressAnimation->setEasingCurve(QEasingCurve::Linear);

    m_backAnimation = new QPropertyAnimation(this, "pos", Button_Press);
    m_backAnimation->setDuration(108);
    m_backAnimation->setEasingCurve(QEasingCurve::Linear);
    connect(Button_Press, &QSequentialAnimationGroup::finished,[this](){
        emit clicked();
    });

}

